<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="React,">





  <link rel="alternate" href="/blog/atom.xml" title="编程那点事" type="application/atom+xml">






<meta name="description" content="React with TypeScript 系列(一) –概述React with TypeScript 系列(二) –基础篇React with TypeScript 系列(三) –实战篇React with TypeScript 系列(四) –数据篇React with TypeScript 系列(五) –Flux  对于React，更多是解决了UI层，也就是对于传统MVC中的View.至于">
<meta name="keywords" content="React">
<meta property="og:type" content="article">
<meta property="og:title" content="React with TypeScript 系列(五) --Flux">
<meta property="og:url" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/index.html">
<meta property="og:site_name" content="编程那点事">
<meta property="og:description" content="React with TypeScript 系列(一) –概述React with TypeScript 系列(二) –基础篇React with TypeScript 系列(三) –实战篇React with TypeScript 系列(四) –数据篇React with TypeScript 系列(五) –Flux  对于React，更多是解决了UI层，也就是对于传统MVC中的View.至于">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127142656806.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127143427824.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127152334758.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127153017750.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127155024981.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127160021826.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127160504595.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127160358656.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127161406779.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127162517544.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127162915769.png">
<meta property="og:updated_time" content="2019-10-03T05:53:45.889Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="React with TypeScript 系列(五) --Flux">
<meta name="twitter:description" content="React with TypeScript 系列(一) –概述React with TypeScript 系列(二) –基础篇React with TypeScript 系列(三) –实战篇React with TypeScript 系列(四) –数据篇React with TypeScript 系列(五) –Flux  对于React，更多是解决了UI层，也就是对于传统MVC中的View.至于">
<meta name="twitter:image" content="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127142656806.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/">





  <title>React with TypeScript 系列(五) --Flux | 编程那点事</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">编程那点事</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lhweb.gitee.io/blog/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="lhweb">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="编程那点事">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">React with TypeScript 系列(五) --Flux</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-09-23T07:41:53+08:00">
                2018-09-23
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/typescript/" itemprop="url" rel="index">
                    <span itemprop="name">typescript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p><a href="https://lhweb.gitee.io/blog/2018/09/22/React-with-TypeScript-%E7%B3%BB%E5%88%97-%E4%B8%80-%E6%A6%82%E8%BF%B0/">React with TypeScript 系列(一) –概述</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-%E7%B3%BB%E5%88%97-%E4%BA%8C-%E2%80%93%E5%9F%BA%E7%A1%80%E7%AF%87/">React with TypeScript 系列(二) –基础篇</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-%E7%B3%BB%E5%88%97-%E4%B8%89-%E5%AE%9E%E6%88%98%E7%AF%87/">React with TypeScript 系列(三) –实战篇</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-%E7%B3%BB%E5%88%97-%E5%9B%9B-%E2%80%93%E6%95%B0%E6%8D%AE%E7%AF%87/">React with TypeScript 系列(四) –数据篇</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/React-with-TypeScript-%E7%B3%BB%E5%88%97-%E4%BA%94-Flux/">React with TypeScript 系列(五) –Flux</a></p>
</blockquote>
<p>对于React，更多是解决了UI层，也就是对于传统MVC中的View.至于Model-Controller 如果要整合React的代码又是如何实现呢？Facebook提出了一个叫做Flux([flʌks])的概念。</p>
<p>在传统MVC中，我们会遇到这样一个问题，随着项目功能和模块的增长，所产生的Model和对应的View就会多起来，而系统的复杂性也随之增加。如下图：<br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127142656806.png" alt><br>当然上图的情况比较极端，但也是一个不能不面对的问题，特别是有双向数据流产生的时候，就更难去解耦了。而Facebook所提供的Flux 是单向数据流的方式呈现，把MVC重新划分成Action ,Dispatcher,Store,View 四个重要的组件。如图：<br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127143427824.png" alt><br>当Action触发时，flux通过Dispatcher进行响应。换句话说，Dispatcher是一个中转站，它管理整个Flux架构的数据枢纽, 这看上去很像我们所说的Controller.而Store更接近我们的Model,负责数据的操作。但它也负责一些状态变化。Dispatcher其实是一个Store的回调注册。当Dispatcher响应Action时，通过已经注册的回调函数，将Action所提供的数据传送给对应的Store，而Store变化时，界面也会同步更新。</p>
<p>听上去有点复杂，我来说一个实例大家就会明白了，还是用回我们的主界面。我们把Facebook一个标准的Flux流图作为参考来说。<br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127152334758.png" alt><br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127153017750.png" alt><br>当要加载页面时，我们需要把Action Creators放在ComponentDidMount方法中，这样才能把最后的数据给页面。<br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127155024981.png" alt>  </p>
<p>Dispatcher要做什么，当然是把对应的Store回调处理好，如所对应的Action和返回的值。这里要说我们需要做一个ActionType为每个不同的Action事件做绑定（如增删改查）。所以在定义Dispatcher时需要定义一个APIActionID的枚举类型。如图：<br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127160021826.png" alt><br>注意：这个枚举类型，分别会为Store和Action作出对应ID。所以大家必须要注意。别以为没用了</p>
<p>还有一个基本的对应类型APIAction<br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127160504595.png" alt> </p>
<p>Dispatcher的最后绑定是和APIAction做绑定</p>
<p><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127160358656.png" alt><br>当我们在componentDidMount中使用Action时，它是通过Dispatcher去寻找Store的回调把数据传给Store保存，从而影响页面的变化的。<br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127161406779.png" alt><br>这里就是Action中的加载，最后是要把返回的结果给Store的，所以会通过Dispatcher绑定actionType,和返回的结果。而在Store中,就只需要对应好actionType即可。下面的的方法就是我们针对不同的actionType，Store所要做的事情。<br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127162517544.png" alt><br>Store涉及侦听回调事件，所以通过EventEmitter去完成.这里我们用到EventEmitter2<br><img src="/blog/2018/09/23/React-with-TypeScript-系列-五-Flux/20160127162915769.png" alt><br>这个时候，只要能匹配好actionType，数据就可以顺利匹配上界面上了。<br>Flux不是一个新的技术，而是一个设计模式。对于Flux更多的是优化项目架构。或者你更习惯MVC/MVVM,但当模块增多的时候你会觉得它是更好的实现。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/React/" rel="tag"># React</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/09/23/React-with-TypeScript-系列-四-–数据篇/" rel="next" title="React with TypeScript 系列(四) –数据篇">
                <i class="fa fa-chevron-left"></i> React with TypeScript 系列(四) –数据篇
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/09/23/mobx介绍/" rel="prev" title="mobx介绍">
                mobx介绍 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">lhweb</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">39</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lhweb</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
